package manage.service;

import m.common.model.util.*;
import m.common.service.Service;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.model.AdminGroup;
import manage.model.AdminGroupLink;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;
import manage.util.DictionaryUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminGroupService extends Service {
	private void setOrgDept(AdminGroup model) throws Exception {
		if("C".equals(model.getType())){
			OrgGroupView m=new OrgGroupView();
			m.setOid(model.getOid());
			if(null!=model.getParent()){
				AdminGroup p=ModelQueryList.getModel(model.getParent(),"*");
				if(null!=p){
					if(ObjectUtil.isEmptyOid(p.getParent())){
						model.setOrg(new OrgGroupView());
						model.getOrg().setOid(p.getOid());
						model.setDept(m);
					}else{
						model.setOrg(p.getOrg());
						model.setDept(p.getDept());
					}
				}else{
					model.setOrg(m);
					model.setDept(null);
				}
			}else{
				model.setOrg(m);
				model.setDept(null);
			}
			ModelUpdateUtil.updateModel(model,"org.oid","dept.oid");
		}
	}
	public String save(AdminGroup model) throws Exception {
		String msg;
		ModelCheckUtil.checkNotNull(model, "num","name");
		ModelCheckUtil.checkUniqueCombine(model, new String[]{"name","parent.oid"});
		ModelCheckUtil.checkUniqueCombine(model, new String[]{"num"});
		if(StringUtil.isSpace(model.getOid())){
			model.setOid(GenerateID.generatePrimaryKey());
			model.setStatus("0");
			ModelUpdateUtil.insertModel(model);
			msg="保存成功";
		}else{
			ModelUpdateUtil.updateModel(model, "name","num","business","description","sort");
			msg="修改成功";
		}
		setOrgDept(model);
		DictionaryUtil.clear(AdminGroup.class);
		CacheUtil.clear(AdminGroup.class);
		CacheUtil.clear(OrgGroupView.class);
		return msg;
	}

	/**
	 * 获取管理员所兼职的机构部门
	 */
	public List<AdminGroup> getOrgList(String adminOid) throws SQLException, MException{
		return ModelQueryList.getModelList(AdminGroup.class, new String[] {"*","parent.*","org.*","dept.*"}, null,
			QueryCondition.and(
				QueryCondition.eq("type", "C"),
				QueryCondition.eq("status", "0"),
				QueryCondition.in("oid", ModelQueryList.instance(AdminGroupLink.class, new String[] {"adminGroup.oid"}, null, QueryCondition.eq("admin.oid", adminOid)))), QueryOrder.asc("sort")
		);
	}

	/**
	 * 验证管理员是否在该部门兼职
	 */
	public boolean verifyOrgGroup(String adminOid,String orgOid) throws SQLException, MException {
		return ModelQueryList.getModelList(AdminGroup.class, new String[] {"oid"}, null,
			QueryCondition.and(
				QueryCondition.eq("type", "C"),
				QueryCondition.eq("status", "0"),
				QueryCondition.eq("oid", orgOid),
				QueryCondition.in("oid", ModelQueryList.instance(AdminGroupLink.class, new String[] {"adminGroup.oid"}, null, QueryCondition.eq("admin.oid", adminOid)))), QueryOrder.asc("sort")
		).size()>0;
	}
	public List<AdminGroup> getList(AdminGroup model) throws SQLException, MException {
		List<QueryCondition> con= new ArrayList<>();
		if(null!=model) {
			if(!StringUtil.isSpace(model.getType()))
				con.add(QueryCondition.eq("type", model.getType()));
			if(!StringUtil.isSpace(model.getStatus()))
				con.add(QueryCondition.eq("status", model.getStatus()));
		}
		return ModelQueryList.getModelList(AdminGroup.class,new String[] {"*","parent.*","org.*","dept.*"},null,
			QueryCondition.and(con.toArray(new QueryCondition[] {})),QueryOrder.asc("sort"));
	}
	public List<AdminGroup> getList4Admin(String adminOid) throws SQLException, MException{
		return ModelQueryList.getModelList(AdminGroup.class,new String[] {"*","parent.*","org.*","dept.*"},null,
			QueryCondition.or(
				QueryCondition.in("oid", ModelQueryList.instance(AdminGroupLink.class, new String[] {"adminGroup.oid"}, null, QueryCondition.eq("admin.oid", adminOid))),
				QueryCondition.in("oid", ModelQueryList.instance(AdminLogin.class, new String[] {"adminGroup.oid"}, null, QueryCondition.eq("oid", adminOid)))),
			QueryOrder.asc("type"),QueryOrder.asc("parent.sort"),QueryOrder.asc("sort")
		);
	}
}
